package com.lwc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwc.bean.GodownEntry;
import com.lwc.bean.GodownEntryDetails;
import com.lwc.bean.GodownEntryQuery;
import com.lwc.bean.MaterialStock;
import com.lwc.common.bean.PageBean;
import com.lwc.mapper.GodownEntryMapper;
import com.lwc.mapper.MaterialStockMapper;
import com.lwc.service.GodownEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class GodownEntryServiceImpl implements GodownEntryService {

@Autowired
    GodownEntryMapper godownEntryMapper;
    @Override
    public PageBean query(GodownEntryQuery godownEntryQuery) {
        PageHelper.startPage(godownEntryQuery.getPage(),godownEntryQuery.getPageSize());
        List<Map> list=godownEntryMapper.query(godownEntryQuery);
        PageInfo info=new PageInfo<>(list);
        return new PageBean<>(godownEntryQuery.getPage(), godownEntryQuery.getPageSize(), info.getTotal(),list);
    }

    @Override
    public List<GodownEntryDetails> getqueryById(int id) {
        return godownEntryMapper.getqueryById(id);
    }
    @Autowired
    MaterialStockMapper materialStockMapper;


    @Transactional
    @Override
    public int getupdatetrue(int id) {
        //同意入库  如有的话商品的话的商品直接 +数量     如果没得话直接 新增商品列表
        //通过订单ID查询订单详情
        List<GodownEntryDetails> order = godownEntryMapper.getqueryById(id);
        List<MaterialStock> goods = materialStockMapper.query(null);
boolean is=false;
int  ids=0;

        for (GodownEntryDetails godownEntryDetails : order) {
            //获取以前的商品
            for (MaterialStock good : goods) {
                //如果入库商品和库存商品一样的话 直接加库存
                if (good.getMaterial_name().equals(godownEntryDetails.getMaterial_name())){
                    is=true;
                    ids=good.getId();
                }else {
                    is=false;
                }
            }
            if (is){
                materialStockMapper.plus(godownEntryDetails.getInto_stock(),ids);
            } else {
                    //新增列表
                    MaterialStock materialStock=new MaterialStock();
                    materialStock.setMaterial_name(godownEntryDetails.getMaterial_name());
                    materialStock.setMaterial_code(godownEntryDetails.getMaterial_code());
                    materialStock.setStock(godownEntryDetails.getInto_stock());
                    materialStock.setUnit(godownEntryDetails.getUnit());
                    materialStock.setType(godownEntryDetails.getType());
                    materialStockMapper.insert(materialStock);
                }
            }
//判断 商品列表有没有
        return godownEntryMapper.getupdatetrue(id);
    }

    @Override
    public int getupdateflaseById(int id, String reason) {

        //增加不通过的原因
    godownEntryMapper.updatereason(reason,id);

        return godownEntryMapper.getupdateflaseById(id);
    }


}
